<?php
/**
 * WB_CacheableObjectClass class
 * 
 * 'modified' and 'expire' fields must be
 * formatted as 'Y-m-d H:i:s'.
 * 
 * @author Jean-Lou Dupont
 * @version @@package-version@@
 * @id $Id: CacheableObjectClass.php 110 2008-03-09 03:27:05Z jldupont $
 */

 //{{include
 require 'web-bloks/StorableClass/StorableClass.php';
 //include}}
 
 class WB_CacheableObjectClass
 	extends WB_StorableClass
 {
 	/**
 	 * If the object can be garbage collected 
 	 * when it expires.
 	 * Will not be stored through the 'StorableClass'
 	 * functionality (because of the '_').
 	 * This field is anyhow updated when the object
 	 * is restored.
 	 * 
 	 * @private
 	 */
 	var $_disposable = true;
	
	/**
	 * Last modification timestamp
 	 * Will not be stored through the 'StorableClass'
 	 * functionality (because of the '_')
	 */
	var $_modified = null;
	
	/**
	 * Expiration timestamp
	 * i.e. when the object expires
 	 * Will not be stored through the 'StorableClass'
 	 * functionality (because of the '_')
 	 * @private
	 */
	var $_expire = null;
	
	/**
	 * Unicity status
	 * Set if an 'ObjStore' found multiple
	 * objects of the same 'id'.
	 * @private 
	 */
	var $_unique = null;
	
	/**
	 * Default expiration timeout (in seconds)
	 * @private
	 */
	static $_defaultExpire = 86400; //1day

	// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%		
	// Constructor/Destructor
	// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%				
 	/**
 	 * Constructor
 	 */
	public function __construct( &$params = null )
	{
		parent::__construct( $params );
	}
	// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%		
	// WB_BaseClass
	// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%				
	public function getClassPath()
	{
		return 'web-bloks/CacheableObjectClass';
	}
	public function getClassVersion()
	{
		return '$Id: CacheableObjectClass.php 110 2008-03-09 03:27:05Z jldupont $';
	}
	// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%		
	// Interface
	// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%				
	/**
	 * Returns the 'disposable' field.
	 * 
	 * @return boolean
	 */
	public function getDisposable()
	{
		return $this->_disposable;
	}
	/**
	 * Sets the 'disposable' field.
	 * 
	 * @return $this
	 * @param $status Object[optional]
	 */
	public function setDisposable( $status = true )	
	{
		//@see WB_BaseClass
		$this->setChangeStatus();
		
		$this->_disposable = $status;
		return $this; // chainable
	}
	/**
	 * Returns the last modification timestamp.
	 * 
	 * @return datetime
	 */
	public function getModified()
	{
		return $this->_modified;
	}	
	/**
	 * Sets the 'modified' field. 
	 * If omitted, then assume 'now' timestamp
	 * 
	 * @return $this
	 * @param $datetime Object[optional]
	 */
	public function setModified( $datetime = null )
	{
		//@see WB_BaseClass
		$this->setChangeStatus();

		$this->_modified = is_null( $datetime ) ?
			date( 'Y-m-d H:i:s' ) : $datetime;
		return $this;
	}
	/**
	 * Returns the 'expire' timestamp.
	 * 
	 * @return $expire timestamp
	 */
	public function getExpire()
	{
		return $this->_expire;
	}
	/**
	 * Sets the 'expire' timestamp.
	 * If omitted, then the default expiration
	 * timeout is used.
	 * 
	 * @return $this
	 * @param $datetime Object
	 */
	public function setExpire( $datetime = null )
	{
		//@see WB_BaseClass
		$this->setChangeStatus();

		$this->_expire = is_null( $datetime ) ?
			date( 'Y-m-d H:i:s', time() + self::$_defaultExpire ) : $datetime;
		
		return $this;
	}
	/**
	 * Returns the 'unicity' status
	 * 
	 * @return boolean
	 */
	public function getUnique()
	{
		return $this->_unique;
	}
	/**
	 * Sets the 'unicity' status.
	 * Defaults to 'true'.
	 * 
	 * @return $this
	 * @param $status Object[optional]
	 */
	public function setUnique( $status = true )
	{
		$this->_unique = $status;
		return $this;
	}
 } // end class declaration